package com.onpositive.knowledge.model.inference;

import java.util.ArrayList;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.semantic.model.api.meta.BaseMeta;
import com.onpositive.semantic.model.api.meta.DefaultMetaKeys;
import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.property.ValueUtils;

public class MergedProperty implements IProperty {

	/**
	 * 
	 */
	private static final long serialVersionUID = -378050454191335429L;
	protected ArrayList<IProperty> report = new ArrayList<IProperty>();

	public MergedProperty() {
		super();
		meta = new BaseMeta();
	}

	protected BaseMeta meta;

	@Override
	public IMeta getMeta() {
		if (report.size()>0){
			return report.get(0).getMeta();
		}
		return meta;
	}

	@Override
	public String getId() {
		return DefaultMetaKeys.getValue(this, DefaultMetaKeys.ID_KEY,
				String.class);
	}

	@Override
	public Object getValue(Object obj) {
		ArrayList<Object> s = new ArrayList<Object>();
		for (IProperty p : report) {
			String pName = p.getMeta().getSingleValue( IPropertyConstants.CHAR_PROPERTY_NAME_LABEL, String.class, null) ;
			Object value = p.getValue(obj);
			Iterable<Object> collection = ValueUtils
					.toCollectionIfCollection(value);
			if (collection != null) {
				for (Object o : collection) {
					s.add(o);
				}
			}
			else{
				s.add(value);
			}
		}
		if (s.size() > 0) {
			return s;
		}
		return null;
	}

	public void add(IProperty p) {
		report.add(p);
	}
	public void remove(IProperty p) {
		report.remove(p);
	}

	public boolean isEmpty() {
		return report.size()==0;
	}
}
